/**
 * 
 */
package com.rf.richfitwheel.admin.sys.controller;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.SysMsgDef;
import com.rf.richfitwheel.admin.sys.model.User;
import com.rf.richfitwheel.admin.sys.service.SysMsgDefService;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hushouquan
 *
 */
@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL + "/msgDef")
public class SysMsgDefController {

	private static Logger log = LoggerFactory.getLogger(SysMsgDefController.class);
	
	@Autowired
    private SysMsgDefService msgDefService;

    /**
     * 分页查询消息规则列表
     */
    @GetMapping("/list")
    public R list(String page, String limit, String dataForm) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("limit", limit);
        params.put("dataForm", dataForm);
        PageUtils pages;
		try {
			pages = msgDefService.queryPage(params);
			return R.ok().put("page", pages);
		} catch (Exception e) {
			log.error("查询失败", e);
			return R.ok().put("page", null);
		}
    }
    
    /**
     * 根据id获取消息规则信息
     *
     * @throws Exception
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) throws Exception {
        SysMsgDef msgDef = msgDefService.selectById(id);
        return R.ok().put("msgDef", msgDef);
    }
	
    /**
     * 保存消息规则信息
     * @param msgDef
     * @return
     */
    @UserOperateLog(module = "系统功能", methods = "save", description = "保存消息规则配置")
    @PostMapping("/save")
    public R save(@RequestBody SysMsgDef msgDef) {
    	try {
			UserVO userVO = UserUtil.getUser();
			Date date = new Date();
			msgDef.setCreater(userVO.getId());
			msgDef.setCreateTime(date);
			msgDef.setModUser(userVO.getId());
			msgDef.setModTime(date);
			msgDefService.save(msgDef);
			return R.ok();
		} catch (Exception e) {
			log.error("保存消息规则失败", e);
			return R.error("保存消息规则失败");
		}
    }

    /**
     * 修改消息规则信息
     * @param msgDef
     * @return
     */
    @UserOperateLog(module = "系统功能", methods = "update", description = "修改消息规则配置")
    @PostMapping("/update")
    public R update(@RequestBody SysMsgDef msgDef) {
    	try {
			UserVO userVO = UserUtil.getUser();
			Date date = new Date();
			msgDef.setModUser(userVO.getId());
			msgDef.setModTime(date);
			msgDefService.update(msgDef);
			return R.ok();
		} catch (Exception e) {
			log.error("更新消息规则失败", e);
			return R.error("更新消息规则失败");
		}
    }

    /**
     * 删除消息规则信息
     * @param ids
     * @return
     */
    @UserOperateLog(module = "系统功能", methods = "delete", description = "删除消息规则配置")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        try {
			msgDefService.delete(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除消息规则失败", e);
			return R.error("删除消息规则失败");
		}
    }
    
    /**
     * 页面信息
     *
     * @throws Exception
     */
    @GetMapping("/pageInfo/{id}")
    public R pageInfo(@PathVariable("id") String id) {
        SysMsgDef msgDef = msgDefService.pageInfo(id);
        return R.ok().put("msgDef", msgDef);
    }

}
